package com.qf.controller;

import com.qf.pojo.User;
import com.qf.service.UserService;
import com.qf.utils.ResultMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService userService;

    @GetMapping("/queryAllUser")
    public ResultMsg queryAllUser(@RequestParam(required = false, defaultValue = "") String name, @RequestParam(required = false, defaultValue = "-1") Integer roleId, @RequestParam(required = false, defaultValue = "-1") Integer status, @RequestParam(required = false, defaultValue = "1") Integer page, @RequestParam(required = false, defaultValue = "10") Integer limit) {
        return userService.queryAllUser(name, roleId, status, page, limit);
    }

    @GetMapping("/queryUser")
    public ResultMsg queryUser(@RequestParam(required = false) Integer userId, @RequestParam(required = false) String username, @RequestParam(required = false) String password, HttpServletRequest request) {
        return userService.queryUser(userId, username, password, request);
    }

    @PutMapping("/updateUser")
    public ResultMsg updateUser(@RequestBody User user) {
        return userService.updateUser(user);
    }

    @PutMapping("/insertUser")
    public ResultMsg insertUser(@RequestBody User user) {
        return userService.insertUser(user);
    }

    @PutMapping("/updateUserStatus")
    public ResultMsg updateUserStatus(@RequestBody Map<String, Object> map) {
        return userService.updateUserStatus((List<Integer>) map.get("userIds"), (Integer) map.get("userStatus"));
    }

    @DeleteMapping("/deleteUser")
    public ResultMsg deleteUser(@RequestBody Map<String, List<Integer>> map) {
        return userService.deleteUser(map.get("userIds"));
    }

    @GetMapping("/loginOut")
    public ResultMsg loginOut(HttpServletRequest request) {
        return userService.logout(request);
    }
}
